<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
session_name(SESSION);
session_start();
$page_id = MiscUtils::getParam('i', 0);
$myPdo = DbUtils::createPdoInst();
$key = json_decode(MiscUtils::convert(MiscUtils::getParam('key', ''), basename(dirname(__FILE__))));
$err = '';
if (!isset($key->t) || !isset($key->t)) {
  $err = '非法链接';
} else if ((time() - $key->s) > 3600) {
  $err = '链接超时，请重新发送密码重置邮件';
} else {
  $cond_vals = new stdClass();
  $cond_vals->c = 't.track_id = :v1';
  $cond_vals->v = array(':v1' => $key->t);
  $tmps = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'client', $cond_vals, NULL, NULL, NULL, NULL, NULL)->d;
  if (count($tmps) == 0) {
    $err = '该账号不存在';
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>Animated Form Switching with jQuery</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="description" content="Expand, contract, animate forms with jQuery wihtout leaving the page" />
        <meta name="keywords" content="expand, form, css3, jquery, animate, width, height, adapt, unobtrusive javascript"/>
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon"/>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
    <script src="js/cufon-yui.js" type="text/javascript"></script>
    <script src="js/ChunkFive_400.font.js" type="text/javascript"></script>
    <script type="text/javascript">
      Cufon.replace('h1',{ textShadow: '1px 1px #fff'});
      Cufon.replace('h2',{ textShadow: '1px 1px #fff'});
      Cufon.replace('h3',{ textShadow: '1px 1px #000'});
      Cufon.replace('.back');
    </script>
    </head>
    <body>
    <div class="wrapper">
      <div class="content">
        <div id="form_wrapper" class="form_wrapper">
          <form class="login active" action="<?php echo basename(__FILE__, '.php'); ?>.server.php" method="post"> 
            <input type="text" name="id" value="<?php echo ((count($tmps) == 0) ? 0 : $tmps[0]->id); ?>" style="display: none;"></input>
            <input type="text" name="action" value="update" style="display: none;"></input>
            <input type="text" name="url" value="http://<?php echo URL . '?i=' . $page_id; ?>" style="display: none;"></input>
            <input type="text" name="from" value="http://<?php echo URL . '?i=' . $page_id . '&script=send&key=' . urlencode(MiscUtils::getParam('key', '')); ?>" style="display: none;"></input>
            <h3>重置密码</h3>
            <?php if ($err != '') { ?>
            <div>
              <label><?php echo $err; ?></label>
            </div>
            <div class="bottom">
              <a href="#" onclick="parent.location.href='http://<?php echo URL . '?i=' . $page_id; ?>';" rel="login" class="linkform">登录</a>
              <div class="clear"></div>
            </div>
            <?php } else { ?>
            <div>
              <label>密码:</label>
              <input type="password" name="pass"/>
              <?php if (isset($_SESSION['pass'])) { ?>
              <span class="error"><?php echo $_SESSION['pass']; ?></span>
              <?php } ?>
            </div>
            <div>
              <label>重复密码:</label>
              <input type="password"  name="pass_re"/>
              <?php if (isset($_SESSION['pass_re'])) { ?>
              <span class="error"><?php echo $_SESSION['pass_re']; ?></span>
              <?php } ?>
            </div>
            <div class="bottom">
              <input type="submit" value="确定"></input>
              <a href="#" onclick="parent.location.href='http://<?php echo URL . '?i=' . $page_id; ?>';" rel="login" class="linkform">返回登录</a>
              <div class="clear"></div>
            </div>
            <?php } ?>
          </form>
        </div>
        <div class="clear"></div>
      </div>
    </div>
    
    <!-- The JavaScript -->
    <script type="text/javascript" src="../../../incls/js/myutils.js"></script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(function() {
          //the form wrapper (includes all forms)
        var $form_wrapper  = $('#form_wrapper'),
          //the current form is the one with class active
          $currentForm  = $form_wrapper.children('form.active'),
          //the change form links
          $linkform    = $form_wrapper.find('.linkform');
            
        //get width and height of each form and store them for later            
        $form_wrapper.children('form').each(function(i){
          var $theForm  = $(this);
          //solve the inline display none problem when using fadeIn fadeOut
          if(!$theForm.hasClass('active'))
            $theForm.hide();
          $theForm.data({
            width  : $theForm.width(),
            height  : $theForm.height()
          });
        });
        
        //set width and height of wrapper (same of current form)
        setWrapperWidth();
        
        /*
        clicking a link (change form event) in the form
        makes the current form hide.
        The wrapper animates its width and height to the 
        width and height of the new current form.
        After the animation, the new form is shown
        */
        $linkform.bind('click',function(e){
//          var $link  = $(this);
//          var target  = $link.attr('rel');
//          $currentForm.fadeOut(400,function(){
//            //remove class active from current form
//            $currentForm.removeClass('active');
//            //new current form
//            $currentForm= $form_wrapper.children('form.'+target);
//            //animate the wrapper
//            $form_wrapper.stop()
//                   .animate({
//                    width  : $currentForm.data('width') + 'px',
//                    height  : $currentForm.data('height') + 'px'
//                   },500,function(){
//                    //new form gets class active
//                    $currentForm.addClass('active');
//                    //show the new form
//                    $currentForm.fadeIn(400);
//                   });
//          });
//          e.preventDefault();
        });
        
        function setWrapperWidth(){
//          $form_wrapper.css({
//            width  : $currentForm.data('width') + 'px',
//            height  : $currentForm.data('height') + 'px'
//          });
        }
        
        /*
        for the demo we disabled the submit buttons
        if you submit the form, you need to check the 
        which form was submited, and give the class active 
        to the form you want to show
        */
//        $form_wrapper.find('input[type="submit"]')
//               .click(function(e){
//                e.preventDefault();
//               });  
      });

      function update() {
        _reset();
        var elm;
        var pass = document.getElementById('pass');
        var pass_re = document.getElementById('pass_re');
        var is_pass = 1;
        if (pass.value.length < 6 || pass.value.length > 16) {
          elm = document.getElementById('pass_err1');
          _err(elm, '长度应在6-16位之间');
          is_pass = 0;
        }
        if (pass.value != pass_re.value) {
          elm = document.getElementById('pass_re_err1');
          _err(elm, '确认密码不对');
          is_pass = 0;
        }
        if (is_pass == 1) {
          var obj = {
            id: <?php echo ((count($tmps) == 0) ? 0 : $tmps[0]->id); ?>,
            password: pass.value
          }
          $.ajax({
            type: 'POST',
            url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
            data: 'action=update&q=' + encodeURIComponent(JSON.stringify(obj)),
            success: function(msg){
              try {
                JSON.parse(msg);
                parent.location.href = 'http://<?php echo URL . '?i=' . $page_id; ?>';
              } catch (ex) {
              }
            }
          });
        }
      }

      function _reset() {
        var errs = document.getElementsByName('error');
        for (var idx in errs) {
          var obj = errs[idx];
          if (obj.style) {
            obj.style.visibility = 'hidden';
          }
        }
      }

      function _err(elm, value) {
        MyUtils.removeChildElements(elm);
        elm.appendChild(document.createTextNode(value));
        elm.style.visibility = 'visible';
      }
    </script>
  </body>
</html>